home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Compressed PICT Info / Compressed PICT Info.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.8 KB  |  305 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Compressed PICT Info.c
  3.  
  4.     Contains:    This snippet shows how to use the StdPix proc to        
  5.                 intercept and display the compression type and depth        
  6.                 of any compressed PICT within a PICT resource.  The            
  7.                 same procedure can be used to gather other information        
  8.                 about the compressed PICT.  Note that all compressed        
  9.                 PICTs get passed into StdPix before getting decom-        
  10.                 pressed and passed to StdBits.    
  11.  
  12.     Written by: Edgar Lee    
  13.  
  14.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  15.  
  16.                 You may incorporate this Apple sample source code into your program(s) without
  17.                 restriction. This Apple sample source code has been provided "AS IS" and the
  18.                 responsibility for its operation is yours. You are not permitted to redistribute
  19.                 this Apple sample source code as "Apple sample source code" after having made
  20.                 changes. If you're going to re-distribute the source, we require that you make
  21.                 it clear in the source that the code was descended from Apple sample source
  22.                 code, but that you've made changes.
  23.  
  24.     Change History (most recent first):
  25.                 7/8/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  26.                 
  27.  
  28. */
  29.  
  30. #include <ImageCompression.h>
  31. #include <Movies.h>
  32. #include <Fonts.h>
  33. #include <Gestalt.h>
  34. #include <Resources.h>
  35. #include <TextUtils.h>
  36.  
  37. /* Constant Declarations */
  38.  
  39. #define    WWIDTH    170
  40. #define    WHEIGHT    220
  41.  
  42. #define WLEFT    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  43. #define WTOP    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  44.  
  45. /* Global Variable Definitions */
  46.  
  47. CodecType    gCodec = 'unkn';
  48. int            gDepth;
  49.  
  50. void initMac();
  51. void checkForQuickTime();
  52.  
  53. void createWindow();
  54. void doEventLoop();
  55.  
  56. void doBottleneckTest();
  57. pascal void myStdPix( PixMapPtr src, Rect *srcRect, MatrixRecordPtr matrix,
  58.                         short mode, RgnHandle mask, PixMapPtr matte,
  59.                         Rect *matteRect, short flags );
  60. pascal void myTextProc( short byteCount, Ptr textBuf, Point numer, Point denom );
  61. pascal void myLineProc( Point newPt );
  62. pascal void myRectProc( GrafVerb verb, Rect *r );
  63. pascal void myRRectProc( GrafVerb verb, Rect *r, short ovalWidth, short ovalHeight );
  64. pascal void myOvalProc( GrafVerb verb, Rect *r );
  65. pascal void myArcProc( GrafVerb verb, Rect *r, short startAngle, short arcAngle );
  66. pascal void myPolyProc( GrafVerb verb, PolyHandle poly );
  67. pascal void myRgnProc( GrafVerb verb, RgnHandle rgn );
  68. pascal void myBitsProc( BitMap *bitPtr, Rect *srcRect, Rect *dstRect,
  69.                         short mode, RgnHandle maskRgn );
  70. void DrawCompressionType( CodecType    codec, int col, int row );
  71. void DrawCompressionDepth( int depth, int col, int row );
  72.         
  73.  
  74. void main()
  75. {
  76.     initMac();
  77.     checkForQuickTime();
  78.     
  79.     createWindow();
  80.     doEventLoop();
  81. }
  82.  
  83. void initMac()
  84. {
  85.     MaxApplZone();
  86.  
  87.     InitGraf( &qd.thePort );
  88.     InitFonts();
  89.     InitWindows();
  90.     InitMenus();
  91.     TEInit();
  92.     InitDialogs( nil );
  93.     InitCursor();
  94.     FlushEvents( 0, everyEvent );
  95. }
  96.  
  97. void checkForQuickTime()
  98. {
  99.     long    version;
  100.     
  101.     if (Gestalt( gestaltQuickTime, &version ) != noErr)
  102.     {
  103.         ParamText( "\pQuickTime not installed.  Please install, then try again.", "\p", "\p", "\p" );
  104.         Alert( 128, nil );
  105.         ExitToShell();
  106.     }
  107. }
  108.  
  109. void createWindow()
  110. {
  111.     Rect        rect;
  112.     WindowPtr    window;
  113.     
  114.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  115.     window = NewCWindow( 0L, &rect, "\pStdPix", true, documentProc,
  116.                             (WindowPtr)-1L, true, 0L );                        
  117.     SetPort( window );
  118.     
  119.     TextMode( srcCopy );
  120.     TextSize( 9 );
  121.     TextFont( kFontIDGeneva );
  122. }
  123.  
  124. pascal void myStdPix( PixMapPtr src, Rect *srcRect, MatrixRecordPtr matrix,
  125.                         short mode, RgnHandle mask, PixMapPtr matte,
  126.                         Rect *matteRect, short flags )
  127. {
  128.     #pragma unused(srcRect,matrix,mode,mask,matte,matteRect,flags)
  129.     ImageDescriptionHandle        desc;
  130.     Ptr                            data;
  131.     long                        bufferSize;
  132.     
  133.     GetCompressedPixMapInfo( src, &desc, &data, &bufferSize, nil, nil );
  134.     gCodec = (**desc).cType;
  135.     gDepth = (**desc).depth;
  136. }
  137.  
  138. pascal
  139.  void myTextProc( short byteCount, Ptr textBuf, Point numer, Point denom )
  140. {
  141.     #pragma unused(byteCount,textBuf,numer,denom)
  142. }
  143.  
  144. pascal void myLineProc( Point newPt )
  145. {
  146.     #pragma unused(newPt)
  147. }
  148.  
  149. pascal void myRectProc( GrafVerb verb, Rect *r )
  150. {
  151.     #pragma unused(verb,r)
  152. }
  153.  
  154. pascal void myRRectProc( GrafVerb verb, Rect *r, short ovalWidth, short ovalHeight )
  155. {
  156.     #pragma unused(verb,r,ovalWidth,ovalHeight)
  157. }
  158.  
  159. pascal void myOvalProc( GrafVerb verb, Rect *r )
  160. {
  161.     #pragma unused(verb,r)
  162. }
  163.  
  164. pascal void myArcProc( GrafVerb verb, Rect *r, short startAngle, short arcAngle )
  165. {
  166.     #pragma unused(verb,r,startAngle,arcAngle)
  167. }
  168.  
  169. pascal void myPolyProc( GrafVerb verb, PolyHandle poly )
  170. {
  171.     #pragma unused(verb,poly)
  172. }
  173.  
  174. pascal void myRgnProc( GrafVerb verb, RgnHandle rgn )
  175. {
  176.     #pragma unused(verb,rgn)
  177. }
  178.  
  179. pascal void myBitsProc( BitMap *bitPtr, Rect *srcRect, Rect *dstRect,
  180.                         short mode, RgnHandle maskRgn )
  181. {
  182.     #pragma unused(bitPtr,srcRect,dstRect,mode,maskRgn)
  183. }
  184.  
  185. void DrawCompressionType( CodecType    codec, int col, int row )
  186. {
  187.     MoveTo(    col, row );
  188.     
  189.     if (codec == 'rpza')
  190.         DrawString( "\pVideo Compression" );
  191.     else if (codec == 'jpeg')
  192.         DrawString( "\pJPEG Compression" );
  193.     else if (codec == 'rle ')
  194.         DrawString( "\pAnimation Compression" );
  195.     else if (codec == 'raw ')
  196.         DrawString( "\pRaw Compression" );
  197.     else if (codec == 'smc ')
  198.         DrawString( "\pGraphics Compression" );
  199.     else
  200.         DrawString( "\pUnknown Compression" );
  201. }
  202.  
  203. void DrawCompressionDepth( int depth, int col, int row )
  204. {
  205.     Str255    string;
  206.     
  207.     NumToString( (long)depth, string );
  208.     
  209.     MoveTo(    col, row );
  210.     DrawString( "\pImage Depth:" );
  211.     
  212.     MoveTo(    col + 70, row );
  213.     DrawString( string );
  214. }
  215.  
  216. void doBottleneckTest()
  217. {
  218.     int            i;
  219.     PicHandle    picture;
  220.     Rect        rect;
  221.     CQDProcs    bottlenecks;
  222.  
  223.     /* Define our own bottlenecks to do nothing. */
  224.     SetStdCProcs( &bottlenecks );
  225.     
  226.     bottlenecks.textProc    = NewQDTextProc(myTextProc);
  227.     bottlenecks.lineProc    = NewQDLineProc(myLineProc);
  228.     bottlenecks.rectProc    = NewQDRectProc(myRectProc);
  229.     bottlenecks.rRectProc    = NewQDRRectProc(myRRectProc);
  230.     bottlenecks.ovalProc    = NewQDOvalProc(myOvalProc);
  231.     bottlenecks.arcProc        = NewQDArcProc(myArcProc);
  232.     bottlenecks.polyProc    = NewQDPolyProc(myPolyProc);
  233.     bottlenecks.rgnProc        = NewQDRgnProc(myRgnProc);
  234.     bottlenecks.bitsProc    = NewQDBitsProc(myBitsProc);
  235.        bottlenecks.newProc1    =NewQDPixProc(myStdPix);        /* pixProc */
  236.     
  237.     for (i = 0; i < Count1Resources( 'PICT' ); i++)
  238.     {
  239.         /* Load & draw pictures from resource. */
  240.         picture = (PicHandle)Get1IndResource( 'PICT', i + 1 );
  241.         
  242.         rect = (**picture).picFrame;
  243.         OffsetRect( &rect, -rect.left, -rect.top );
  244.         OffsetRect( &rect, 10, ((rect.bottom - rect.top) + 10) * i + 10 );
  245.                 
  246.         DrawPicture( picture, &rect );
  247.         
  248.         /* Install our custom bottlenecks to intercept any compressed images. */
  249.         (*(qd.thePort)).grafProcs = (QDProcs *)&bottlenecks;
  250.          DrawPicture( picture, &((**picture).picFrame) );
  251.          
  252.         /* Switch back to the default procs. */
  253.           (*(qd.thePort)).grafProcs = 0L;
  254.      
  255.         /* Free any memory used by the picture. */
  256.         ReleaseResource( (Handle)picture );
  257.         
  258.         /* Display the compression type. */
  259.         DrawCompressionType( gCodec, rect.right + 10, rect.top + 10 );
  260.         DrawCompressionDepth( gDepth, rect.right + 10, rect.top + 25 );
  261.         gCodec = 'unkn';
  262.     }
  263. }
  264.  
  265. void doEventLoop()
  266. {
  267.     EventRecord event;
  268.     WindowPtr   window;
  269.     short       clickArea;
  270.     Rect        screenRect;
  271.  
  272.     for (;;)
  273.     {
  274.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  275.         {
  276.             if (event.what == mouseDown)
  277.             {
  278.                 clickArea = FindWindow( event.where, &window );
  279.                 
  280.                 if (clickArea == inDrag)
  281.                 {
  282.                     screenRect = (**GetGrayRgn()).rgnBBox;
  283.                     DragWindow( window, event.where, &screenRect );
  284.                 }
  285.                 else if (clickArea == inContent)
  286.                 {
  287.                     if (window != FrontWindow())
  288.                         SelectWindow( window );
  289.                 }
  290.                 else if (clickArea == inGoAway)
  291.                     if (TrackGoAway( window, event.where ))
  292.                         return;
  293.             }
  294.             else if (event.what == updateEvt)
  295.             {
  296.                 window = (WindowPtr)event.message;    
  297.                 SetPort( window );
  298.                 
  299.                 BeginUpdate( window );
  300.                 doBottleneckTest();
  301.                 EndUpdate( window );
  302.             }
  303.         }
  304.     }
  305. }